//
//  HTClassTool.swift
//  QuitSmokingApp
//
//  Created by   on 2024/3/6.
//
import UIColor_Hex_Swift
import Foundation
import Toaster

class HTClassTool {
    static let STATIC_background = UIColor("#15171B")
    /// 获取屏幕宽度
    static var STATIC_screenWidth: CGFloat {
        return UIScreen.main.bounds.width
    }
    
    /// 获取屏幕高度
    static var STATIC_screenHeight: CGFloat {
        return UIScreen.main.bounds.height
    }
    
    /// 获取屏幕尺寸
    static var STATIC_screenSize: CGSize {
        return UIScreen.main.bounds.size
    }
    
    /// 获取安全区域宽度
    static var STATIC_safeAreaWidth: CGFloat {
        if #available(iOS 11.0, *) {
            return UIApplication.shared.keyWindow?.safeAreaInsets.left ?? 0
        } else {
            return 0
        }
    }
    
    /// 获取安全区域高度
    static var STATIC_safeAreaHeight: CGFloat {
        if #available(iOS 11.0, *) {
            return UIApplication.shared.keyWindow?.safeAreaInsets.top ?? 0
        } else {
            return 0
        }
    }
    /// 获取状态栏的高度
    static var STATIC_statusBarHeight: CGFloat {
        if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene {
            return windowScene.statusBarManager?.statusBarFrame.height ?? 0
        } else {
            return 0
        }
    }
    
    /// 获取导航栏的高度
    static var STATIC_navigationBarHeight: CGFloat {
        return 44
    }
    
    /// 获取导航栏总高度
    static var STATIC_navigationBarTotalHeight: CGFloat {
        return 44 + STATIC_statusBarHeight
    }
    
    /// 获取keyWindow
    static var STATIC_keyWindow: UIWindow? {
        let var_windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene
        let var_keyWindow = var_windowScene?.windows.first(where: { $0.isKeyWindow })
        return var_keyWindow
    }
    
    /// 底部安全边距
    static var STATIC_bottomSafeAreaHeight: CGFloat {
        guard let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
              let statusBarManager = windowScene.statusBarManager else {
            return 0
        }
        
        let statusBarFrame = statusBarManager.statusBarFrame
        let statusBarHeight = statusBarFrame.height
        let safeAreaBottom = statusBarHeight > 20 ? 34 : 0
        return CGFloat(safeAreaBottom)
    }
    
    /// 获取导航默认颜色
    static func ht_getNavigationBack() -> UIColor {
        if HTClassStorage.ht_getNavBg() == "1" {
            return UIColor("#9F3125")
        } else if HTClassStorage.ht_getNavBg() == "2" {
            return UIColor("#EE7921")
        } else if HTClassStorage.ht_getNavBg() == "3" {
            return UIColor("#7A979D")
        }
        return UIColor("#14B4DA")
    }
    
    static func ht_toast(_ var_msg: String) {
        ToastView.appearance().backgroundColor = .clear
        Toast(attributedText: NSAttributedString(string: var_msg, attributes:
                                                    [NSAttributedString.Key.backgroundColor: UIColor.clear,
                                                     NSAttributedString.Key.foregroundColor: UIColor.red]),duration: 1.5).show()
    }
    
    static func ht_successToast(_ var_msg: String) {
        ToastView.appearance().backgroundColor = .clear
        Toast(attributedText: NSAttributedString(string: var_msg, attributes:
                                                    [NSAttributedString.Key.backgroundColor: UIColor.clear,
                                                     NSAttributedString.Key.foregroundColor: UIColor("#42e695")]),duration: 1.5).show()
    }
    
    static func ht_isValid(_ var_str: String) -> Bool {
        let var_regex = #"^\d*\.?\d+$"#
        return NSPredicate(format: "SELF MATCHES %@", var_regex).evaluate(with: var_str)
    }
    static func ht_isValidInteger(_ var_input: String) -> Bool {
        if let _ = Int(var_input) {
            return true
        } else {
            return false
        }
    }
    static func ht_topVC() -> UIViewController? {
        return ht_getTopViewController()
    }
    
    static func ht_getTopViewController(_ var_base: UIViewController? = STATIC_keyWindow?.rootViewController) -> UIViewController? {
        if let nav = var_base as? UINavigationController {
            return ht_getTopViewController( nav.visibleViewController)
        }
        if let var_tab = var_base as? UITabBarController {
            if let var_selected = var_tab.selectedViewController {
                return ht_getTopViewController(var_selected)
            }
        }
        if let var_presented = var_base?.presentedViewController {
            return ht_getTopViewController(var_presented)
        }
        return var_base
    }
    
    /// 获取年月日字符串
    /// - Returns: <#description#>
    static func ht_obtainYearMonthDay() -> String {
        let var_dateFormatter = DateFormatter()
        var_dateFormatter.dateFormat = "yyyy-MM-dd"
        let var_dateString = var_dateFormatter.string(from: Date())
        return var_dateString
    }
    
    /// 获取年月
    /// - Returns:
    static func ht_obtainYearMonth() -> String {
        let var_dateFormatter = DateFormatter()
        var_dateFormatter.dateFormat = "yyyy.MM"
        let var_dateString = var_dateFormatter.string(from: Date())
        return var_dateString
    }
    
    static func ht_convertToFixedFormat(_ var_yearMonth: String) -> String {
        let var_components = var_yearMonth.components(separatedBy: ".")
        
        if var_components.count == 2, let year = Int(var_components[0]), let month = Int(var_components[1]) {
            let var_formattedMonth = String(format: "%02d", month)
            return "\(year).\(var_formattedMonth)"
        }
        
        // 默认返回原始输入
        return var_yearMonth
    }
    
    
    static func ht_getDay(_  var_dateString: String) -> Int? {
        // 创建日期格式化器
        let var_dateFormatter = DateFormatter()
        var_dateFormatter.dateFormat = "yyyy-MM-dd"

        // 将日期字符串转换为日期
        guard let var_date = var_dateFormatter.date(from: var_dateString) else {
          return nil
        }

        // 获取日期的天
        let var_day = Calendar.current.component(.day, from: var_date)

        return var_day
      }

}
